<?php
class Shipper extends CI_Controller {

	var $page = 'shipper';
	var $action = '';
	var $displayShipper = false;


	var $message = '';

	public function index(){
		$query = $this->db->query("select s.* ,
				(select concat(firstname,' ',lastname) from users u where u.id = s.updatedby) as updatedby,
				(select concat(firstname,' ',lastname) from users u where u.id = s.createdby) as createdby
				
				from shipper as s where valid = 1");
		//$data['User'] = "meeee";
		//echo "SET.......".$this->session->userdata('session_id');
		$data['totalRecords'] =  $query->num_rows();
			
		if($this->message != '')
		$data['successMessage'] = $this->message;
			
			
		$this->admin($data,$query);

	}

	public function add(){
		$this->action = 'add';
		//$this->displayShipper = true;
		$this->index();
	}


	public function save(){

		$name = $_REQUEST['name'];
		$description = $_REQUEST['description'];

		if($this->validateFields() == FALSE){
			$data['errorMessage'] = validation_errors();
			$this->admin($data, null);
			return;
		}
			
		$isUpdate = false;
			
			

		if(isset($_REQUEST['id']))
		$isUpdate  = true;
			
		if($isUpdate){
			$query = $this->db->query("UPDATE  shipper SET  name =  '$name',description =  '$description' , updatedon =  NOW() WHERE  id =".$_REQUEST['id']);

			$this->message = "Shipper was successfully Updated.";

			$this->createLog('SHIPPER' , 'UPDATED' ,  "Updated Shipper Record where id = ".$_REQUEST['id']);

		}else{
			$this->db->query("INSERT INTO  shipper (id ,name ,description".$this->creationFields().")
								  VALUES (NULL ,  '$name',  '$description'".$this->creationValues().")");

			$this->createLog('SHIPPER' , 'CREATED' ,  "Created new Shipper");

			$this->message = "New Shipper was successfully Created.";
		}

		//
		//$this->createLog('PAGES', )
			
		$this->index();

	}

	public function delete($id){

		$data['deleteId'] = $id;
			
		$query = $this->db->query("update shipper set valid = false  where id = $id limit 1" );
			
		$this->createLog('SHIPPER' , 'DELETED' ,  "Deleted Shipper Record where id = $id");
			
		$this->index();
			
	}

	public function edit($id){
			
		//$this->page = 'edit'.$this->page;
			
		$this->action = 'edit';
			
		$query = $this->db->query("select * from shipper where id = $id and valid= 1 limit 1" );
		//$row =
		$data['shipper'] = $query->row();

			
		$this->admin($data,$query);
			
	}

}